//引入高德地图SDK核心类
var Amap = require("../../urtils/amap-wx.js")
//实例化高德地图API核心类
var GDmap = new Amap.AMapWX({
  key: '6551c449afd21c1d573fa552cd096ca9'
})
var app = getApp()
Page({
  data: {
    addressName: null, //地址
    addressDesc: null, //地址描述
    latitude: 23.12908,
    longitude: 113.26436,
    markers: [],
    gender: null,
    myImg: null,
    dkList: []
  },
  onLoad: function() {
    this.setData({
      myImg: app.globalData.myImg
    })
  },
  //获取当前定位信息
  getLocation: function() {
    var that = this
    //获取用户头像
    wx.getUserInfo({
      success: function(res) {
        wx.setStorageSync("myImg", res.userInfo.avatarUrl)
        wx.setStorageSync("gender", res.userInfo.gender == 1 ? "../../imgs/boy.png" : "../../imgs/boy.png")
        app.globalData.myImg = res.userInfo.avatarUrl
        app.globalData.gender = res.userInfo.gender == 1 ? "../../imgs/boy.png" : "../../imgs/boy.png"
        that.setData({
          myImg: app.globalData.myImg,
          gender: app.globalData.gender,
        })
      }
    })
    //获取定位信息
    wx.getLocation({
      success: function(res) {
        var myLocation = new Array;
        myLocation[0] = {
          id: 1,
          latitude: res.latitude,
          longitude: res.longitude,
        }
        that.setData({
          markers: myLocation,
          latitude: res.latitude,
          longitude: res.longitude,
        })
        var dwdkTime = new Date()
        var dwdkYear = (dwdkTime.getFullYear()).toString()
        var dwdkMounth = (dwdkTime.getMonth() < 10 ? '0' + dwdkTime.getMonth() : dwdkTime.getMonth()).toString()
        var dwdkDate = (dwdkTime.getDate() < 10 ? '0' + dwdkTime.getDate() : dwdkTime.getDate()).toString()
        var dwdkHour = (dwdkTime.getHours() < 10 ? '0' + dwdkTime.getHours() : dwdkTime.getHours()).toString()
        var dwdkMinute = (dwdkTime.getMinutes() < 10 ? '0' + dwdkTime.getMinutes() : dwdkTime.getMinutes()).toString()
        var dwdkTimes = dwdkYear + dwdkMounth + dwdkDate + dwdkHour + dwdkMinute
        console.log(dwdkTimes)
        var GDJW = res.longitude + "," + res.latitude
        GDmap.getRegeo({
          location: GDJW,
          success: function(data) {
            var dkList = new Array()
            var dkListTemp = new Array()
            dkList[0] = {
              addressName: data[0].name,
              addressDesc: data[0].desc,
              dwdkTimes: dwdkTimes,
              dwdkHours: dwdkHour,
              dwdkMinutes: dwdkMinute
            }
            if (that.data.dkList.length < 2) {
              that.data.dkList.push(dkList[0])
              that.setData({
                dkList: that.data.dkList
              })
            } else {
              wx.showToast({
                title: '每日定位打卡数已用完',
                icon: "none"
              })
            }
            console.log(that.data.dkList)
            this.mapCtx = wx.createMapContext('myMap')
          }
        })
      },
    })
  },

})